using System;
using Unity.Mathematics;
using UnityEngine;

namespace GpuHud
{
    [Serializable]
    public class GpuHudHealthBarStyleAsset : GpuHudStyleBase<HealthBarStyleShaderData>
    {
        //Cache
        [SerializeField]
        private float m_UVRect2Z;
        
#if UNITY_EDITOR
        public Sprite HealthBar_Bar
        {
            set
            {
                if (value != null && value.texture == GpuHudFacade.GpuFacade.Settings.HealthBarStyleMaterial.mainTexture)
                {
                    m_ShaderOriginData[m_StateIdx].UVRectBar = GpuHudUtils.GetUVRect(value);
                    
                    //缓存下Z（也就是Rect.width/Texture.width)
                    m_UVRect2Z = m_ShaderOriginData[m_StateIdx].UVRectBar.z;
                }
                else
                {
                    m_ShaderOriginData[m_StateIdx].UVRectBar = default;
                }
            }
        }

        public Sprite HealthBar_Bg
        {
            set
            {
                if (value != null && value.texture == GpuHudFacade.GpuFacade.Settings.HealthBarStyleMaterial.mainTexture)
                {
                    m_ShaderOriginData[m_StateIdx].UVRectBg = GpuHudUtils.GetUVRect(value);
                }
            }
        }
# endif

        public override void RebuildShaderData(GpuHudStyleDataAccessor<HealthBarStyleShaderData> accessor)
        {
            base.RebuildShaderData(accessor);
/*            var healthBarAccessor = (GpuHudHealthBarAccessor) accessor;
            // Position
            healthBarAccessor.ShaderCacheData[0].Position = ShaderOriginData.Position + (float3)accessor.m_dataAccessor.Position;
            
            healthBarAccessor.Percent = Mathf.Clamp01(healthBarAccessor.Percent);
            healthBarAccessor.ShaderCacheData[0].Percent = ShaderOriginData.UVRectBg.x + ShaderOriginData.UVRectBg.z * healthBarAccessor.Percent;
            // 这里可能为Infinity
            healthBarAccessor.ShaderCacheData[0].UVRectBar.z = m_UVRect2Z/healthBarAccessor.Percent;
            
            //临时手动刷新, 全局刷新未开启, SetPercent返回临时数据
            healthBarAccessor.SetShaderData(accessor.ShaderCacheData[0]);//Set Self Cache Data*/
        }
    }
}
